{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Convolution-like operations\n",
      "===========================\n",
      "\n",
      "Users frequently want to break an array up into overlapping chunks, then\n",
      "apply the same operation to each chunk. You can generate a dynamical\n",
      "power spectrum, for example, by taking an FFT of each chunk, or you can\n",
      "construct a convolution using a dot product. Some of these operations\n",
      "already exist in numpy and scipy, but others don't. One way to attack\n",
      "the problem would be to make a matrix in which each column was a\n",
      "starting location, and each row was a chunk. This would normally require\n",
      "duplicating some data, potentially a lot of data if there's a lot of\n",
      "overlap, but numpy's striding can be used to do this. The simplification\n",
      "of striding doesn't come for free; if you modify the array, all shared\n",
      "elements will be modified. Nevertheless, it's a useful operation. Find\n",
      "attached the code, [segmentaxis.py](files/attachments/SegmentAxis/segmentaxis.py) . Example usage:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [1]: import numpy as N\n",
      "In [2]: import segmentaxis\n",
      "In [3]: a = N.zeros(30)\n",
      "In [4]: a[15] = 1\n",
      "In [5]: filter = N.array([0.1,0.5,1,0.5,0.1])\n",
      "In [6]: sa = segmentaxis.segment_axis(a,len(filter),len(filter)-1)\n",
      "In [7]: sa\n",
      "Out[7]:\n",
      "array([[ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  1.],\n",
      "       [ 0.,  0.,  0.,  1.,  0.],\n",
      "       [ 0.,  0.,  1.,  0.,  0.],\n",
      "       [ 0.,  1.,  0.,  0.,  0.],\n",
      "       [ 1.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.],\n",
      "       [ 0.,  0.,  0.,  0.,  0.]])\n",
      "In [8]: N.dot(sa[::2,:],filter)\n",
      "Out[8]:\n",
      "array([ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0.5,  0.5,  0. ,  0. ,  0. ,\n",
      "        0. ,  0. ])\n",
      "In [9]: N.dot(sa[1::2,:],filter)\n",
      "Out[9]:\n",
      "array([ 0. ,  0. ,  0. ,  0. ,  0. ,  0.1,  1. ,  0.1,  0. ,  0. ,  0. ,\n",
      "        0. ,  0. ])\n",
      "In [10]: N.dot(sa,filter)\n",
      "Out[10]:\n",
      "array([ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,\n",
      "        0.1,  0.5,  1. ,  0.5,  0.1,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,\n",
      "        0. ,  0. ,  0. ,  0. ])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
